package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yqg.protection.common.Result;
import com.yqg.protection.entity.InspectAddrCode;
import com.yqg.protection.entity.RecordInspection;
import com.yqg.protection.entity.RecordMaintenance;
import com.yqg.protection.mapper.InspectAddrCodeMapper;
import com.yqg.protection.mapper.RecordInspectionMapper;
import com.yqg.protection.mapper.RecordMaintenanceMapper;
import com.yqg.protection.service.InspectAddrCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * InspectAddrCodeController 前端控制器
 *
 * @author zxf
 * @since 2024-11-18
 */
@RestController
@RequestMapping("/inspect-addr-code")
public class InspectAddrCodeController {

    @Resource
    private InspectAddrCodeService inspectAddrCodeService;

    @Autowired
    private InspectAddrCodeMapper inspectAddrCodeMapper;

    @Autowired
    private RecordInspectionMapper recordInspectionMapper;

    @Autowired
    private RecordMaintenanceMapper recordMaintenanceMapper;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    public Result save(@RequestBody InspectAddrCode inspectAddrCode) {
        if (inspectAddrCode.getLatitude() != null && inspectAddrCode.getLongitude() != null) {
            inspectAddrCode.setIsPosition("1");
        } else {
            inspectAddrCode.setIsPosition("0");
        }
        inspectAddrCode.setIsDelete(0);
        return Result.success(inspectAddrCodeService.saveOrUpdate(inspectAddrCode));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        InspectAddrCode inspectAddrCode = inspectAddrCodeService.getById(id);
        inspectAddrCode.setIsDelete(1);
        return Result.success(inspectAddrCodeService.saveOrUpdate(inspectAddrCode));
    }

    /**
     * 查询所有(下拉选择)
     */
    @GetMapping("/all")
    public Result findAll(Integer level,
                          Long organizationId,
                          String isAllClient,
                          @RequestParam List<Long> clientIds) {
        List<InspectAddrCode> list = inspectAddrCodeMapper.findAll(level, organizationId, isAllClient, clientIds);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(inspectAddrCodeService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String keyWord,
                           Integer level,
                           Long organizationId,
                           String isAllClient,
                           @RequestParam List<Long> clientIds) {
        pageNum = pageNum * pageSize - pageSize;
        List<InspectAddrCode> inspectAddrCodes = inspectAddrCodeMapper.findPage(pageNum, pageSize, keyWord, level, organizationId, isAllClient, clientIds);
        Long count = inspectAddrCodeMapper.findPageCount(keyWord, level, organizationId, isAllClient, clientIds);
        HashMap<String, Object> map = new HashMap<>();
        map.put("record", inspectAddrCodes);
        map.put("total", count);
        return Result.success(map);
    }

    @GetMapping("/newAddrData")
    public Result newAddrData(Long id) {
        InspectAddrCode inspectAddrCode = inspectAddrCodeMapper.getDataById(id);
        LambdaQueryWrapper<RecordInspection> recordInspectionQueryWrapper = new LambdaQueryWrapper<>();
        recordInspectionQueryWrapper.eq(RecordInspection::getAddress, id);
        recordInspectionQueryWrapper.orderByDesc(RecordInspection::getDateTime);
        List<RecordInspection> recordInspections = recordInspectionMapper.selectList(recordInspectionQueryWrapper);
        LambdaQueryWrapper<RecordMaintenance> recordMaintenancenQueryWrapper = new LambdaQueryWrapper<>();
        recordMaintenancenQueryWrapper.eq(RecordMaintenance::getAddress, id);
        recordMaintenancenQueryWrapper.orderByDesc(RecordMaintenance::getDateTime);
        List<RecordMaintenance> recordMaintenances = recordMaintenanceMapper.selectList(recordMaintenancenQueryWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", inspectAddrCode.getId());
        map.put("addrName", inspectAddrCode.getAddrName());
        map.put("clientName", inspectAddrCode.getClientName());
        map.put("address", inspectAddrCode.getAddress());
        map.put("inspectCount", recordInspections.size());
        map.put("maintenanceCount", recordMaintenances.size());
        if (recordInspections.size() > 0) {
            map.put("inspectUserName", recordInspections.get(0).getUserName());
            map.put("inspectTelephone", recordInspections.get(0).getTelephone());
            map.put("inspectDateTime", recordInspections.get(0).getDateTime());
            map.put("inspectInformation", recordInspections.get(0).getInformation());
        }
        if (recordMaintenances.size() > 0) {
            map.put("maintenanceUserName", recordMaintenances.get(0).getUserName());
            map.put("maintenanceTelephone", recordMaintenances.get(0).getTelephone());
            map.put("maintenanceDateTime", recordMaintenances.get(0).getDateTime());
            map.put("maintenanceInformation", recordMaintenances.get(0).getInformation());
        }
        return Result.success(map);
    }
}

